import os

os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'
import tensorflow as tf
from tensorflow.python import keras
from tensorflow.python.keras.preprocessing.image import load_img, img_to_array
from tensorflow.python.keras.layers import Dense, Flatten, Input
from tensorflow.python.keras.models import Sequential, Model


def main():
    # # 加载图片
    # # target_size: 是将图片的形状改为指定形状
    # image = load_img(path="./img/1.jpg", target_size=(300, 300))
    #
    # print(image)
    #
    # # 图片转换为数组
    # image_array = img_to_array(image)
    #
    # print(image_array)

    # # 获取数据集
    # (x_train, y_train), (x_test, y_test) = keras.datasets.cifar100.load_data()
    # print(x_train.shape)
    # print(y_train.shape)

    # ----------------- 建立模型 ----------
    # 1. 通过Sequential建立模型
    print("Sequential")
    model1 = Sequential([
        # 改变输入数据的形状: (784,) --> (28, 28)
        Flatten(input_shape=(28, 28)),
        # 建立一层隐藏层
        Dense(128, activation=tf.nn.relu),
        # 建立第二层隐藏层
        Dense(64, activation=tf.nn.relu),
        # 。。。
        # 建立输出层，输出层的激活函数必须是softmax
        Dense(10, activation=tf.nn.softmax)
    ])

    print("model1", model1)
    print("model1.layers:", model1.layers)
    print("model1.input:", model1.input)
    print("model1.output", model1.output)
    print("model1.inputs:", model1.inputs)
    print("model1.outputs", model1.outputs)
    print("model1.inputs[0]:", model1.inputs[0])
    print("model1.outputs[0]", model1.outputs[0])
    """
    model1.input: Tensor("flatten_input:0", shape=(?, 28, 28), dtype=float32)
    model1.output Tensor("dense_2/Softmax:0", shape=(?, 10), dtype=float32)
    model1.inputs: [<tf.Tensor 'flatten_input:0' shape=(?, 28, 28) dtype=float32>]
    model1.outputs [<tf.Tensor 'dense_2/Softmax:0' shape=(?, 10) dtype=float32>]
    model1.inputs[0]: Tensor("flatten_input:0", shape=(?, 28, 28), dtype=float32)
    model1.outputs[0] Tensor("dense_2/Softmax:0", shape=(?, 10), dtype=float32)
    """
    print("model1.summary:\n", model1.summary())
    print('*' * 50)

    print("Model")

    # 2. 使用Model建立模型
    data = Input(shape=(784,))
    print(data)
    out = Dense(64)(data)
    print(out)
    model_second = Model(inputs=data, outputs=out)

    print(model_second)
    print('*' * 50)


if __name__ == '__main__':
    main()
